var netUtil = require('/../../utils/netUtil.js')
var app = getApp();

Page({
  
  data:{
 			myTeaArray : [],
 			searchWord : null   //查询的关键字
  },
  onLoad:function(options){
  	var that = this;
  	//检查是否已经 注册
 		app.checkHasRegister(null,function() {
 			app.getUserOpedId(that.getBalanceQuery);
 		});
  },
  onReady:function(){
  
  },
  onShow:function(){
    
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },

  onUnload:function(){
    // 页面关闭

  },
  
  inputSearchWord : function(e) {
  	
  	var searchWord = e.deatil.value;
  	this.setData({
  		searchWord : searchWord
  	});
  },
  
  getBalanceQuery : function() {
 
  	var that = this;
  	var params = {
  		data : {
  			goodsName : that.data.searchWord
  		},
  		method : 'wechat.wisen.whbalance.balance.query',
  		openId : app.globalData.userInfo.openid
  	};
  	
  	netUtil.buildRequest(that,'/wisen/api/wechat/ret',params,{
  		onPre: function() {
				netUtil.showLoadingDialog(that);
			},
			onSuccess : function(resp) {
				
				netUtil.hideLoadingDialog(that);
				
				//如果请求成功
				if(resp.ret == app.globalData.respSuccessCode) {
					
					var myTeaArray = resp.data;
          var total=0;
          var pack=0;
					
						//为返回的数据这只 套餐的值(月，季，年) ===== start
					var comboArray = app.globalData.comboArray;
					for(var i=0;i<myTeaArray.length;i++) {
						for(var j=0;j<comboArray.length;j++) {
							if(comboArray[j].comboKey == myTeaArray[i].combo) {
								myTeaArray[i].comboValue = comboArray[j].comboValue;
               
							}
						}
            total = total + myTeaArray[i].totalQuantity;
            if (myTeaArray[i].packageQuantity != null && myTeaArray[i].packageQuantity>0){
              pack = pack + myTeaArray[i].packageQuantity;
            }
					}
					
					that.setData({
            total: total,
            pack:pack,
						myTeaArray : myTeaArray
					})
					
				} else {
					netUtil.showAlertDialog("提示", resp.message, false, "确定", null, null);
				}
				
			},
			onError: function(msgCanShow, code, hiddenMsg) {
        console.log("fail");
				netUtil.hideLoadingDialog(that);
				netUtil.showAlertDialog("提示", msgCanShow, false, "确定", null, null);
			}
  		
  	}).send();
  }
	
	
})